﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.DataProxy.DbConnector;

namespace Enter.DataProxy.Statistics
{
	public class UserManager : BaseManager
	{
		/// <summary>
		/// 添加用户 延长用户时间
		/// </summary>
		/// <param name="name"></param>
		/// <param name="Month"></param>
		/// <param name="isTry"></param>
		/// <returns>用户ID</returns>
		public int Insert_User(string name, int Month, bool isTry)
		{
			string sqltxt = @"declare @userid int;
							declare @endtime datetime;
							declare @oldtime datetime;
							declare @defimg varchar(100);
							set @userid=0;
							select @userid=id,@endtime=endtime from [userinfo] where username=@username and state=0;
							if @userid>0 
							begin
								set @oldtime=@endtime;
								if @endtime<=GETDATE()
								begin
									set @endtime=getdate();
								end
								set @endtime=dateadd(M,@month,@endtime);
	
								update [userinfo] set state=0,vip=0,endtime=@endtime;
							end
							else
							begin
							set @oldtime=getdate();
							set @endtime=dateadd(M,@month,getdate());
							insert into [userinfo] (username,title,state,vip,addtime,endtime,isTry,defpv)
							values (@username,@username,0,0,getdate(),@endtime,@isTry,0);
							set @userid=scope_identity();

							select top 1 @defimg=imgsrc from [imglist] where state=0;

							insert into userdef (userid,imgdef,imglink)
							values (@userid,@defimg,'');

							insert into userStatistics (userid,Yesterdaytime,losttime)
							values ( @userid,getdate(),getdate());

							end

							insert into uservip (userid,[month],oldtime,newtime)
							values (@userid,@month,@oldtime,@endtime);
							
							select @userid;
							";
			var db = base.CreateDbWriter();
			ParameterCollection parameters = db.CreateParamCollection(3);
			parameters.Append("username", name);
			parameters.Append("isTry", isTry);
			parameters.Append("month", Month);

			int result = db.ExecuteScalar<int>(sqltxt, parameters);
			return result;
		
		}

		/// <summary>
		/// 更新用户默认图片
		/// </summary>
		/// <param name="userid"></param>
		/// <param name="imgid"></param>
		/// <returns></returns>
		public string Update_UserImg(int userid, int imgid)
		{
			string sqltxt = @"declare @defimg varchar(100);
							select @defimg=imgsrc from [imglist] where state=0 and id=@imgid;
							update userdef set imgdef=@defimg where userid=@userid;
							select @defimg;";

			var db = base.CreateDbWriter();
			ParameterCollection parameters = db.CreateParamCollection(2);
			parameters.Append("userid", userid);
			parameters.Append("imgid", imgid);

			string result = db.ExecuteScalar<string>(sqltxt, parameters);
			return result;
		}

		/// <summary>
		/// 更新用户基本信息
		/// </summary>
		/// <param name="userid"></param>
		/// <param name="title"></param>
		/// <param name="defpv"></param>
		public void Update_UserInfo(int userid, string title, int defpv)
		{
			string sqltxt = @"update userinfo set defpv=@defpv,title=@title where id=@userid;";
			var db = base.CreateDbWriter();
			ParameterCollection parameters = db.CreateParamCollection(3);
			parameters.Append("userid", userid);
			parameters.Append("title", title);
			parameters.Append("defpv", defpv);

			db.ExecuteNonQuery(sqltxt, parameters);

		}

		/// <summary>
		/// 用户登录 
		/// </summary>
		/// <param name="username"></param>
		/// <returns>用户ID</returns>
		public int Show_UserLogin(string username)
		{
			string sqltxt = @"select ID from userinfo where username=@username and state=0 and endtime>GETDATE();";
			var db = base.CreateDbWriter();
			ParameterCollection parameters = db.CreateParamCollection(1);
			parameters.Append("username", username);

			int result = db.ExecuteScalar<int>(sqltxt, parameters);
			return result;
		}

		/// <summary>
		/// 查询用户过期时间
		/// </summary>
		/// <param name="uid"></param>
		/// <returns></returns>
		public DateTime Show_UserVipTime(int uid)
		{
			string sqltxt = "select endtime from userinfo where id=@uid";
			var db = base.CreateDbWriter();
			ParameterCollection parameters = db.CreateParamCollection(1);
			parameters.Append("uid", uid);

			DateTime result = db.ExecuteScalar<DateTime>(sqltxt, parameters);
			return result;
		
		}

	}
}
